package cn.yangke.system.controller;

import cn.yangke.basic.util.AjaxResult;
import cn.yangke.basic.util.PageList;
import cn.yangke.system.domain.DictionaryDetail;
import cn.yangke.system.query.DictionaryDetailQuery;
import cn.yangke.system.service.IDictionaryDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author YangKe
 * @Date 2022/6/5 9:50
 */
@RequestMapping("/dicDetail")
@RestController
public class DictionaryDetailController {
    @Autowired
    private IDictionaryDetailService dictionaryDetailService;

    /**
     * 基于id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public DictionaryDetail queryById(@PathVariable("id") Long id){
        return dictionaryDetailService.queryById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<DictionaryDetail> queryAll(){
        return dictionaryDetailService.queryAll();
    }

    /**
     * 分页加高级查询
     * @param query
     * @return
     */
    @PostMapping
    public PageList<DictionaryDetail> queryPage(@RequestBody DictionaryDetailQuery query){
        return dictionaryDetailService.queryPage(query);
    }

    /**
     * 新增或者修改
     * @param dictionaryDetail
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryDetail dictionaryDetail){
        try {
            if(dictionaryDetail.getId() == null){
                dictionaryDetailService.save(dictionaryDetail);
            }else {
                dictionaryDetailService.update(dictionaryDetail);
            }
            return AjaxResult.getAjax();

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .getAjax()
                    .setSuccess(false)
                    .setMessage("操作失败,我们正在殴打程序员,请稍后!");
        }

    }

    /**
     * 指定id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            dictionaryDetailService.removeById(id);
            return AjaxResult.getAjax();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .getAjax()
                    .setSuccess(false)
                    .setMessage("操作失败,我们正在殴打程序员,请稍后!");
        }
    }

    @PatchMapping
    public AjaxResult batchRemove(@RequestBody Long[] ids){
        try {
            dictionaryDetailService.batchRemove(ids);
            return AjaxResult.getAjax();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .getAjax()
                    .setSuccess(false)
                    .setMessage("操作失败,我们正在殴打程序员,请稍后!");
        }
    }



}
